package cn.paplink.deviceinfo;

import android.graphics.Point;
import android.hardware.display.IDisplayManager;
import android.os.Build;
import android.os.IBinder;
import android.view.DisplayInfo;
import android.view.IWindowManager;

import java.lang.reflect.Method;

/**
 *
 * @author hdz
 * @date 2018/12/29
 */

public class Util {


    /**
     * http://www.top139.com/diy/19.html
     */
    public static String getSystemProperty(String key, String def) {
        try {
            final ClassLoader cl = ClassLoader.getSystemClassLoader();
            final Class<?> SystemProperties = cl.loadClass("android.os.SystemProperties");
            final Class<?>[] paramTypes = new Class[]{String.class, String.class};
            final Method get = SystemProperties.getMethod("get", paramTypes);
            final Object[] params = new Object[]{key, def};
            return (String) get.invoke(SystemProperties, params);
        } catch (Exception e) {
            return def;
        }
    }

    public static boolean hasNavigationBar() {
        try {
            Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            IWindowManager wm = IWindowManager.Stub.asInterface((IBinder)getServiceMethod.invoke(null, new Object[]{"window"}));
            if (wm != null) {
                return wm.hasNavigationBar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //getDevDisplaySize
    public static Point getResolution() {
        Point displaySize = new Point(0,0);
        try {
            Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
            IWindowManager wm;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                wm = IWindowManager.Stub.asInterface((IBinder)getServiceMethod.invoke(null, new Object[]{"window"}));
                wm.getInitialDisplaySize(0, displaySize);
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
                DisplayInfo di = IDisplayManager.Stub.asInterface((IBinder) getServiceMethod.invoke(null, new Object[]{"display"})).getDisplayInfo(0);
                displaySize.x = ((Integer) DisplayInfo.class.getDeclaredField("logicalWidth").get(di)).intValue();
                displaySize.y = ((Integer) DisplayInfo.class.getDeclaredField("logicalHeight").get(di)).intValue();
            } else {
                wm = IWindowManager.Stub.asInterface((IBinder)getServiceMethod.invoke(null, new Object[]{"window"}));
                wm.getRealDisplaySize(displaySize);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return displaySize;
    }

    public static Point getAppDisplaySize() {
        Point displaySize = new Point(0,0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            try {
                Method getServiceMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
                DisplayInfo di = IDisplayManager.Stub.asInterface((IBinder) getServiceMethod.invoke(null, new Object[]{"display"})).getDisplayInfo(0);
                displaySize.x = ((Integer) DisplayInfo.class.getDeclaredField("appWidth").get(di)).intValue();
                displaySize.y = ((Integer) DisplayInfo.class.getDeclaredField("appHeight").get(di)).intValue();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return displaySize;
    }

    public static String getAndroidVer(int i) {
        switch (i) {
            case Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1:
                return "4.0.3,4.0.4";
            case Build.VERSION_CODES.JELLY_BEAN:
                return "4.1,4.1.1";
            case Build.VERSION_CODES.JELLY_BEAN_MR1:
                return "4.2,4.2.2";
            case Build.VERSION_CODES.JELLY_BEAN_MR2:
                return "4.3";
            case Build.VERSION_CODES.KITKAT:
                return "4.4";
            case Build.VERSION_CODES.KITKAT_WATCH:
                return "4.4W";
            case Build.VERSION_CODES.LOLLIPOP:
                return "5.0";
            case Build.VERSION_CODES.LOLLIPOP_MR1:
                return "5.1";
            case Build.VERSION_CODES.M:
                return "6.0,6.0.1";
            case Build.VERSION_CODES.N:
                return "7.0";
            case Build.VERSION_CODES.N_MR1:
                return "7.1,7.1.1";
            case Build.VERSION_CODES.O:
                return "8.0";
            case 27:
                return "8.1";
            case 28:
                return "9.0";
            default:
                return "";
        }
    }
}
